package system.base;

import framework.core.*;
import system.SelectManager;
import system.data.*;

/*
 Author    :Jackey
 CreateTime:2008-02-15 13:29:25
 */
public class MenuInfo extends MenuInfoBase {

    public static String PARENT_CODE_TOP = "0";
    public static int PARENT_CODE_TOP_INT = 0;
    // ----system generated start----//
    public MenuInfo() {

    }

    private static void delete(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        delete(con, TABLE_NAME, conditions);
    }

    public static MenuInfo fetch() {
        return new MenuInfo();
    }

    public static MenuInfo copy(MenuInfo srcObj) {
        MenuInfo obj = fetch();
        obj.copyBase(srcObj);
        return obj;
    }

    public static MenuInfo fetch(DBConnection con, String id) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(ID, id);

        return fetch(con, conditions);
    }

    public static MenuInfo fetch(DBConnection con, Conditions conditions) throws Exception {
        DBResult result = null;
        try {
            result = DBEngine.doSelect(con, "select * from " + TABLE_NAME, conditions);
            java.sql.ResultSet rs = result.getRs();
            MenuInfo obj = null;
            if (rs.next()) {
                obj = fetch(rs);
            }
            return obj;
        } finally {
            if (result != null)
                result.clear();
        }
    }

    public static MenuInfo fetch(java.sql.ResultSet rs) throws Exception {
        MenuInfo obj = new MenuInfo();
        obj.initObject(rs);
        return obj;
    }

    protected void getUnique(DBConnection con) throws Exception {
        setId(DBEngine.getId(con));
    }

    // ----system generated end----//

    // ----add by Jackey start----//

    // ----add by Jackey end----//

    // ----add by Sindy start----//
    public static ObjectList fetchbyParentCode(DBConnection con) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(PARENT_CODE, PARENT_CODE_TOP);

        ObjectList list = MenuInfo.fetch().search(con, conditions,MENU_CODE);

        return list;
    }

    public static ObjectList fetchSubs(DBConnection con) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(PARENT_CODE,Conditions.NOT_EQUAL, PARENT_CODE_TOP);

        ObjectList list = MenuInfo.fetch().search(con, conditions,DISPLAY_ORDER);

        return list;
    }    
    
    public static ObjectList fetchbyCode(int code,DBConnection con) throws Exception {
        Conditions conditions = new Conditions();

        conditions.add(PARENT_CODE, code);

        ObjectList list = MenuInfo.fetch().search(con, conditions);

        return list;
    }
    
    public static ObjectList getListSubByParentCode(ObjectList listSubs,int parentCode) throws Exception
    {
        ObjectList list = ObjectList.getList();
        for(int i=0;i<listSubs.size();i++)
        {
            MenuInfo m = (MenuInfo)listSubs.get(i);
            if (m.getParentCode() == parentCode)
            {
                list.add(m);
            }
        }
        
        return list;
    }

    public static MenuInfo getListSubById(ObjectList listSubs,String menuId) throws Exception
    {
        MenuInfo m = null;
        for(int i=0;i<listSubs.size();i++)
        {
            m = (MenuInfo)listSubs.get(i);
            if (m.getId().equalsIgnoreCase(menuId))
            {
                return m;
            }
        }
        return m;
    }  
    
    // ----add by Sindy end----//

}
